home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_300 / 353_01 / chap12.txt < prev    next >
Text File  |  1992-01-18  |  26KB  |  589 lines

  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7.                                                        Chapter 12
  8.                                            FLYAWAY ADVENTURE GAME
  9.  
  10. Now that you have learned lots of things about C++, and know how
  11. to write and use a single isolated class, you have the problem of
  12. how to build a program with several classes that work together to
  13. accomplish some task.  After some amount of thought, it seems that
  14. an adventure game would be a good candidate for a relatively large
  15. example program.  It has lots of input and output and requires a
  16. good deal of flexibility while running since there are so many
  17. things that can be included in the game as obstacles, mazes, items
  18. to find, and puzzles to solve.
  19.  
  20. The adventure game presented in this chapter is unique as far as
  21. I know, since I have never heard of another adventure game
  22. featuring an airport.  The location is not nearly as important as
  23. the code used to get through the airport.  You are advised to play
  24. the game to get familiar with what the code does, then study the
  25. code to see how it works.  Finally, you are given an assignment to
  26. extend the code which will be the real test of whether you
  27. understand its operation.
  28.  
  29.  
  30.  
  31. PLAYING THE GAME
  32. _________________________________________________________________
  33.  
  34. Prior to studying the source code for this game,  ===============
  35. it would be to your advantage to spend some time    FLYAWAY.EXE
  36. playing the game to get familiar with what the    ===============
  37. game does.  Load the file FLYAWAY.EXE and begin
  38. the adventure through the airport.  The
  39. executable file is precompiled for you so you can begin executing
  40. the program before you have to compile and link the whole thing.
  41. The entire program is composed of 15 files and will take a little
  42. effort on your part to properly compile and link it, but that will
  43. come later.
  44.  
  45. If you have played adventure games before, sometimes called
  46. interactive fiction, you should begin trying various commands to
  47. find your way through the airport to your proper plane.  If you
  48. have not played before, a few hints are in order concerning how to
  49. play the game.
  50.  
  51. The object of the game is to get to your proper plane on time so
  52. you can fly away to your vacation.  Of course there a few obstacles
  53. and problems along the way and they will be brought up at the
  54. appropriate time.  It will be up to you to solve the puzzles
  55. associated with each problem.  To add a little excitement, you only
  56. have about twenty-five minutes to get to your plane, with each move
  57.  
  58.                                                         Page 12-1
  59.  
  60.                               Chapter 12 - Flyaway Adventure Game
  61.  
  62. taking a minute, so you must hurry.  Of course, just getting to the
  63. plane on time is not enough, there are a few additional
  64. requirements.  You will find what they are as you progress through
  65. the game.  You will probably find it necessary to restart the game
  66. many times before you arrive at your destination unscathed and on
  67. time.
  68.  
  69.  
  70.  
  71. THE METHOD OF PLAY
  72. _________________________________________________________________
  73.  
  74. The method of play is extremely simple.  You simply wander around
  75. the airport looking for things to do and places to go.  You move
  76. around the airport by giving the system commands to go in a
  77. direction with four choices available, north, south, east, or west.
  78. You can abbreviate any of these four direction commands to the
  79. first letter only, and you can use either upper or lower case.  The
  80. system may move you to another area of the airport, or it may tell
  81. you that you can't go that way.  Try loading the game now and
  82. typing the four directions once each to see what happens.  If this
  83. is not clear, enter the word help to get you started.
  84.  
  85. In addition to moving around, you can pick up items or ask for more
  86. information in any of the rooms.  Try telling the system to look
  87. around the room and see what additional information it gives you
  88. for each room, some of the clues for solving the puzzle are given
  89. in the clues issued in response to a look command.  Another
  90. important command is inventory which will give you a list of the
  91. items you possess at any given point in time.  Type the word
  92. inventory at this time to see what items you possess.
  93.  
  94. The remainder of the commands consist of two words, a verb and a
  95. noun.  These can be given in either order, since the system is
  96. smart enough to know the difference, and additional words may be
  97. given following the legal words.  If you give the system a command
  98. that is not in its limited vocabulary, it will tell you it doesn't
  99. understand that word.  Try telling the system to drop an item you
  100. possess, or get an item that is located in the room you are
  101. currently in.
  102.  
  103. Several friends have played this game with no more knowledge than
  104. you have been given.  One solved it in 40 minutes, but most took
  105. about an hour to complete the game.  After you play the game for
  106. awhile, return to the text and we will study the source code for
  107. the game.  The entire source code for the game is on your
  108. distribution disk.  The game was purposely kept small so the code
  109. could be easily grasped by a programming student.  There is no
  110. reason the game could not have been made much larger by the
  111. addition of more rooms, items, and traps.  You may choose to do
  112. just that to gain experience in working with C++.
  113.  
  114.  
  115.  
  116.  
  117.                                                         Page 12-2
  118.  
  119.                               Chapter 12 - Flyaway Adventure Game
  120.  
  121.  
  122. A FEW SPECIAL CONSTANTS
  123. _________________________________________________________________
  124.  
  125. The file named FLYAWAY.H contains the             ===============
  126. definitions for TRUE and FALSE as well as the        FLYAWAY.H
  127. enumerated type defining the legal dictionary of  ===============
  128. words for use in playing the game.  The list was
  129. started at a value of 1 so the value of zero can
  130. be used to indicate that the word in question was not in the
  131. library and hence not a legal word for use with the game.
  132.  
  133. The #ifndef in line 5 is required because this header file is
  134. included in many of the other files and if it is included more than
  135. once, there will be a multiple definition, and hence an error.  A
  136. class only needs to be defined once, so after it is defined by one
  137. of the includes, the name ITEMS_H will be defined and any other
  138. defines will be ignored.  This is necessary because of the separate
  139. compilation capability of C++.  This was described in more detail
  140. near the end of chapter 7.
  141.  
  142.  
  143. THE FIRST CLASS - clock
  144. _________________________________________________________________
  145.  
  146. Examine the file named CLOCK.H for the            ===============
  147. definition of the clock class.  This is the           CLOCK.H
  148. class for the game clock, and only one instance   ===============
  149. of this class will be used.  It will be used for
  150. the object time_of_day defined in line 23 of
  151. FLYAWAY.CPP.
  152.  
  153. The class is very simple, consisting of only two variables, the
  154. hour and the minute, and four methods.  The first method is the
  155. constructor used to initialize the clock to 8:51 as you can see if
  156. you refer to the implementation of this class in the file named
  157. CLOCK.CPP.  The next two methods are used to get the current values
  158. of the two variables.  The final method is much more interesting
  159. since it does more.  It updates the time of day clock and outputs
  160. the user prompt to ask for the next command.  This may not be the
  161. best place to output the user prompt since this class is devoted
  162. to the time of day and associated operations, but this was chosen
  163. as the place to do it since the time of day is part of the user
  164. prompt.  You will notice that the clock was initialized to 8:51,
  165. but the first time output was 8:52 when you played the game.  In
  166. order to simplify the coding later, when we need to decide if we
  167. made it to the plane on time, the time was incremented at the
  168. beginning of each game move.  The time is therefore the same when
  169. the command is entered and when it is executed, hence the time is
  170. incremented prior to even the first output.
  171.  
  172. The clock class is by far the simplest class in the adventure game